package controller;

import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.table.DefaultTableModel;
import model.Admin;
import model.Group;
import model.Group_Teacher_Subject;
import model.Student;
import model.Subject;
import model.Teacher;
import model.query.HQL;

/**
 *
 * @author cska
 * 
 */

public class AdminCtrl {

    public AdminCtrl() {
    }

    public DefaultListModel groupList() {
        DefaultListModel model = new DefaultListModel();
        int index = 0;
        for (Group group : HQL.getSelect().groups())
            model.add(index++, group.toString());
        return model;
    }

    public DefaultComboBoxModel groupCombo() {
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        for (Group group : HQL.getSelect().groups())
            model.addElement(group.toString());
        return model;
    }

    public DefaultListModel studentList(Group group) {
        DefaultListModel model = new DefaultListModel();
        int index = 0;
        if (group != null)
            for (Student student : group.getStudents())
                model.add(index++, index+". "+student.toString());
        return model;
    }

    public DefaultTableModel teacherTable() {
        String headers[] = {"Ф.И.О.","Должность","Пользователь","Пароль"};
        DefaultTableModel model = new DefaultTableModel(headers, 0);
        for (Teacher teacher : HQL.getSelect().teachers()) {
            String row[] = {teacher.getFio(), teacher.getRank(), teacher.getLogin(), teacher.getPass()};
            model.addRow(row);
        }
        return model;
    }

    public DefaultComboBoxModel teacherCombo() {
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        for (Teacher teacher : HQL.getSelect().teachers())
            model.addElement(teacher.toString());
        return model;
    }

    public DefaultListModel subjectList() {
        DefaultListModel model = new DefaultListModel();
        int index = 0;
        for (Subject subject : HQL.getSelect().subjects())
            model.add(index++, subject.toString());
        return model;
    }

    public DefaultComboBoxModel subjectCombo() {
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        for (Subject subject : HQL.getSelect().subjects())
            model.addElement(subject.toString());
        return model;
    }

    public DefaultTableModel gtsTable() {
        String headers[] = {"Группа", "Преподаватель", "Предмет"};
        DefaultTableModel model = new DefaultTableModel(headers, 0);
        for (Group_Teacher_Subject gts : HQL.getSelect().gtses()) {
            String row[] = {gts.getGroup().toString(), gts.getTeacher().toString(), gts.getSubject().toString()};
            model.addRow(row);
        }
        return model;
    }

    public DefaultTableModel adminTable() {
        String headers[] = {"Пользователь","Пароль"};
        DefaultTableModel model = new DefaultTableModel(headers,0);
        for (Admin admin : HQL.getSelect().admins()) {
            String row[] = {admin.getLogin(), admin.getPass()};
            model.addRow(row);
        }
        return model;
    }

    public Group getGroup(int index) {
        int i = 0;
        for (Group group : HQL.getSelect().groups())
            if (i == index)
                return group;
            else
                i++;
        return null;
    }

    public Student getStudent(int gIndex, int sIndex) {
        Group group = getGroup(gIndex);
        int i = 0;
        for (Student student : group.getStudents())
            if (i == sIndex)
                return student;
            else
                i++;
        return null;
    }

    public Teacher getTeacher(int index) {
        int i = 0;
        for (Teacher teacher : HQL.getSelect().teachers())
            if (i == index)
                return teacher;
            else
                i++;
        return null;
    }

    public Subject getSubject(int index) {
        int i = 0;
        for (Subject subject : HQL.getSelect().subjects())
            if (i == index)
                return subject;
            else
                i++;
        return null;
    }

    public int getGroupIndex(int index) {
        Group_Teacher_Subject gts = getGts(index);
        int i = 0;
        for (Group group : HQL.getSelect().groups())
            if (group.getId().equals(gts.getGroup().getId()))
                return i;
            else
                i++;
        return 0;
    }

    public int getTeacherIndex(int index) {
        Group_Teacher_Subject gts = getGts(index);
        int i = 0;
        for (Teacher teacher : HQL.getSelect().teachers())
            if (teacher.getId().equals(gts.getTeacher().getId()))
                return i;
            else
                i++;
        return 0;
    }

    public int getSubjectIndex(int index) {
        Group_Teacher_Subject gts = getGts(index);
        int i = 0;
        for (Subject subject : HQL.getSelect().subjects())
            if (subject.getId().equals(gts.getSubject().getId()))
                return i;
            else
                i++;
        return 0;
    }

    public Group_Teacher_Subject getGts(int index) {
        int i = 0;
        for (Group_Teacher_Subject gts : HQL.getSelect().gtses())
            if (i == index)
                return gts;
            else
                i++;
        return null;
    }

    public Admin getAdmin(int index) {
        int i = 0;
        for (Admin admin : HQL.getSelect().admins())
            if (i == index)
                return admin;
            else
                i++;
        return  null;
    }

    public void addGroup(Group group) {
        HQL.getInsert().group(group);
    }

    public void updateGroup(Group group) {
        HQL.getUpdate().group(group);
    }

    public void deleteGroup(Group group) {
        HQL.getDelete().group(group);
    }

    public void addStudent(Student student) {
        HQL.getInsert().student(student);
    }

    public void deleteStudent(Student student) {
        HQL.getDelete().student(student);
    }

    public void updateStudent(Student student) {
        HQL.getUpdate().student(student);
    }

    public void addTeacher(Teacher teacher) {
        HQL.getInsert().teacher(teacher);
    }

    public void deleteTeacher(Teacher teacher) {
        HQL.getDelete().teacher(teacher);
    }
    
    public void updateTeacher(Teacher teacher) {
        HQL.getUpdate().teacher(teacher);
    }

    public void addSubject(Subject subject) {
        HQL.getInsert().subject(subject);
    }

    public void deleteSubject(Subject subject) {
        HQL.getDelete().subject(subject);
    }

    public void updateSubject(Subject subject) {
        HQL.getUpdate().subject(subject);
    }

    public void addGts(int gIndex, int tIndex, int sIndex) {
        HQL.getInsert().gts(new Group_Teacher_Subject(getGroup(gIndex), getTeacher(tIndex), getSubject(sIndex)));
    }

    public void deleteGts(Group_Teacher_Subject gts) {
        HQL.getDelete().gts(gts);
    }

    public void updateGts(int index, int gIndex, int tIndex, int sIndex) {
        Group_Teacher_Subject gts = getGts(index);
        gts.setGroup(getGroup(gIndex));
        gts.setTeacher(getTeacher(tIndex));
        gts.setSubject(getSubject(sIndex));
        HQL.getUpdate().gts(gts);
    }

    public void addAdmin(Admin admin) {
        HQL.getInsert().admin(admin);
    }

    public void deleteAdmin(Admin admin) {
        HQL.getDelete().admin(admin);
    }

    public void updateAdmin(Admin admin) {
        HQL.getUpdate().admin(admin);
    }

}
